package cn.copera.dyy.web;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.copera.dyy.common.utils.StringUtils;
import cn.copera.dyy.common.web.BaseController;
import cn.copera.dyy.entity.Order;
import cn.copera.dyy.entity.Shop;
import cn.copera.dyy.entity.Track;
import cn.copera.dyy.entity.UserShop;
import cn.copera.dyy.service.ExchangeRuleService;
import cn.copera.dyy.service.LogService;
import cn.copera.dyy.service.OrderService;
import cn.copera.dyy.service.ShopService;
import cn.copera.dyy.service.TrackService;
import cn.copera.dyy.service.UserService;
import cn.copera.dyy.service.UserShopService;

@Controller
@RequestMapping(value = "${adminPath}/track")
public class TrackController extends BaseController{

	@Autowired
	private TrackService trackService;
	@Autowired
	private ShopService shopService;
	@Autowired
	private UserService userService;
	@Autowired
	private LogService logService;
	@Autowired
	private ExchangeRuleService ruleService;
	@Autowired
	private UserShopService userShopService;
	@Autowired
	private OrderService orderService;
	
	@RequestMapping(value="/save",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> save(@RequestParam(required = false) String nickName,String ruleId,String shopId,String longitude,String latitude,String trackId){
		Map<String, Object> map = new HashMap<String,Object>();
		try {
			UserShop userShop = userShopService.findUserShop(shopId);
			if (userShop != null) {

				DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
				Shop shop = shopService.get(shopId);
				Track track;
				if (StringUtils.isNotBlank(trackId)) {
					track = trackService.get(trackId);
					track.setCount(track.getCount()+1);
					track.setUsableCount(track.getUsableCount()+1);
					trackService.update(track);
				}else {
					track = new Track();
					track.setShop(shop);
					track.setNickName(nickName);
					track.setCreateDate(new Date());
					track.setCreateName(shop.getLoginName());
					track.setCreateDateStr(format.format(new Date()));
					track.setLongitude(longitude);
					track.setLatitude(latitude);
					track.setCount(1);
					track.setUsableCount(1);
					Date date = new Date();
			        Calendar cal = Calendar.getInstance();
			        cal.setTime(date);
			        cal.add(Calendar.MONTH, Integer.parseInt(shop.getValidDate()));
			        track.setValidDate(cal.getTime());
			        track.setState("0");
			        trackService.save(track);
				}
				
//				User user = userService.findUserByNickName(nickName);
//				if (user != null) {
//					if (user.getUserCardPoint() == null) {
//						user.setUserCardPoint(1);
//					}else {
//						user.setUserCardPoint(user.getUserCardPoint()+1);
//					}
//					if (user.getCurrentCardPoint() == null) {
//						user.setCurrentCardPoint(1);
//					}else {
//						user.setCurrentCardPoint(user.getCurrentCardPoint()+1);
//					}
//					userService.save(user);
//				}
				
				if (userShop.getCountPoint() != null) {
					userShop.setCountPoint(userShop.getCountPoint()+1);
				}else {
					userShop.setCountPoint(1);
				}	
				UserShop shop2 = userShopService.findUserShopByOpenid(shopId, nickName);
				shop2.setOperate(new Date());
				userShopService.update(shop2);
				
				Order order = orderService.findOrder(shopId, nickName);
				if(order!=null){
					order.setTime(new Date());
					orderService.save(order);
				}
//				Log log = new Log();
//				log.setCheckorno("yes");
//				log.setContent("消费一次，获取1个卡点");
//				log.setType("1");
//				log.setOpenid(nickName);
//				log.setShop(shop);
//				log.setTrackTime(format.format(new Date()));
//				
//				logService.save(log);
				
				map.put("message", "保存成功");
				map.put("trackToday", trackService.count4Today(shopId));
				map.put("trackId", track.getId());
			}else{
				map.put("message", "该消费者未关注kaka足迹微信平台，无法记录足迹");
			}
		} catch (Exception e) {
			map.put("message", "保存失败，请重新扫描");
		}
		return map;
	}
	
	//今天的足迹
	@RequestMapping(value="/findTrackToday",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> findTrackToday(@RequestParam(required = false) String shopId){
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("trackTodayList", trackService.findTrackToday(shopId));
		return map;
	}	
	//最近3天的足迹
	@RequestMapping(value="/findTrackOld",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> findTrackOld(@RequestParam(required = false) String shopId){
		return trackService.findTrackOld(shopId);
	}		
}
